import Loadable from 'react-loadable'
import Loading from '../components/Loading';
import _ from 'lodash';
import * as Img from '../resources/index';

const Login = Loadable({loader: () => import('../pages/Login/index'), loading: Loading});
const NotFound = Loadable({loader: () => import('../pages/NotFound/index'), loading: Loading});

// 首页
const HomeMain = Loadable({loader: () => import('../pages/Home/index'), loading: Loading});
const DashBoard = Loadable({loader: () => import('../pages/Home/System/DashBoard'), loading: Loading});
const AccountSet = Loadable({loader: () => import('../pages/Home/System/AccountSet'), loading: Loading});
const PasswordSet = Loadable({loader: () => import('../pages/Home/System/PasswordSet'), loading: Loading});
const SystemJournal = Loadable({loader: () => import('../pages/Home/System/SystemJournal'), loading: Loading});
const ModelTool = Loadable({loader: () => import('../pages/Home/System/ModelTool'), loading: Loading});

// 后台用户
const AdminUser = Loadable({loader: () => import('../pages/Home/System/AdminUser'), loading: Loading});
const OperaLog = Loadable({loader: () => import('../pages/Home/System/OperaLog'), loading: Loading});
const AdminUserAdd = Loadable({loader: () => import('../pages/Home/System/AdminUserAdd'), loading: Loading});

export const SystemConfig = {
    systemName: '投票系统后台管理',
    systemShortName: '投票系统',
    systemLogo: Img.Logo,

    // 默认页
    index: {path: '/', name: '工作台', exact: true, component: DashBoard},
    model: 0, // 0=简单导航模式，1=高级导航模式

    // 配置菜单页
    menu: [
        {
            path: '/system/', name: '首页', icon: 'home', exact: true, component: DashBoard, subMenu: [
                {path: '/system/dashboard', icon: 'dashboard', name: '工作台', exact: true, component: DashBoard},
                {path: '/system/account-set', name: '账户设置', icon: 'setting', exact: true, component: AccountSet},
                {path: '/system/password-set', name: '密码修改', icon: 'setting', exact: true, component: PasswordSet},
                {
                    path: '/system/system-journal',
                    pass: true,
                    name: '登录日志',
                    icon: 'solution',
                    exact: true,
                    component: SystemJournal
                },
                {
                    path: '/system/model-tool',
                    name: '模型工具',
                    pass: process.env.NODE_ENV === 'production',
                    icon: 'tool',
                    exact: true,
                    component: ModelTool
                },
            ],
        },

        {
            path: '/system/user-mgr', name: '后台用户', exact: true, component: AdminUser, subMenu: [
                {path: '/system/admin-user', name: '后台用户管理', exact: true, component: AdminUser},
                {path: '/system/opera-log', pass: true, name: '操作日志', exact: true, component: OperaLog},
                {path: '/system/admin-user-add', pass: true, name: '添加后台用户', exact: true, component: AdminUserAdd},
            ]
        },

    ]
};

export function getMainNav() {
    return _.take(SystemConfig.menu, SystemConfig.menu.length).filter(f => !f.pass).map(nav => (
        {
            path: nav.path,
            exact: !!nav.exact,
            name: nav.name,
        }
    )).filter(f => f.path)
}

export function formatPath(path) {
    return '/' + path.split('/').filter(f => !!f).join('/');
}

export function getPath(pathname) {
    let pathsStack = [];
    let stack = _.take(SystemConfig.menu, SystemConfig.menu.length).map(m => {
        m.deep = [{name: m.name, path: m.path}];
        return m;
    });

    while (stack.length > 0) {
        let nav = stack.shift();

        if (nav.path && !~nav.path.indexOf('http') && formatPath(nav.path) === formatPath(pathname)) {
            pathsStack = nav.deep;
            break;
        }

        if (nav.subMenu && nav.subMenu.length > 0) {
            stack.push(...nav.subMenu.map(m => {
                m.deep = nav.deep.concat([{name: m.name, path: m.path}]);
                return m;
            }));
        }
    }

    return pathsStack;
}

export function genRouter() {
    let routeBase = [
        {path: '/login', name: '登录页', exact: true, component: Login},
        {
            // 系统主页
            path: '/', name: '首页', component: HomeMain, routes: []
        },
        {path: '*', exact: true, component: NotFound},
    ];

    let homeIndex = routeBase.findIndex(r => r.path === '/');

    // 插入首页
    routeBase[homeIndex].routes.push(SystemConfig.index);

    let stack = _.take(SystemConfig.menu, SystemConfig.menu.length);

    while (stack.length > 0) {
        let nav = stack.shift();

        if (nav.path && !~nav.path.indexOf('http')) {
            routeBase[homeIndex].routes.push({
                path: nav.path,
                exact: !!nav.exact,
                name: nav.name,
                component: nav.component
            })
        }

        if (nav.subMenu && nav.subMenu.length > 0) {
            stack.push(...nav.subMenu);
        }
    }

    routeBase[homeIndex].routes.push({path: '*', name: '没有权限', exact: true, component: NotFound});
    return routeBase;
}
